package com.atguigu.yygh.hosp.controller;


import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.atguigu.yygh.common.util.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author hong
 * @since 2022-10-08
 */
// RestController将返回的数据转换为JSON返回给浏览器
@RestController
@Api(tags = "医院设置相关接口")
@RequestMapping("/admin/hosp/hospitalSet")
@Slf4j
@CrossOrigin // 解决跨域问题 只是一种局部方案
public class HospitalSetController {

    //使用REST风格的请求方式:
    // GET:查询
    // POST:添加
    // PUT:修改
    // DELETE:删除
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "锁定与解锁")
    @PostMapping("/status/{id}/{status}")
    public R updateStatus(@PathVariable Long id, @PathVariable Integer status){
        HospitalSet hospitalSet = new HospitalSet();
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/delete")
    public R batchDelete(@RequestBody List<Integer> ids){
        hospitalSetService.removeByIds(ids);
        return R.ok();
    }

    @ApiOperation(value = "根据ID查询医院设置")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable Integer id){
        return R.ok().data("item",hospitalSetService.getById(id));
    }

    @ApiOperation(value = "根据ID修改医院设置")
    @PutMapping("/update")
    public R updateById(@RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    @ApiOperation(value = "新增接口")
    @PostMapping("/save")
    public R save(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);// 默认状态1表示正常 0表示不正常
        // 当前时间戳+随机数+MD5
        Random random = new Random();
        String key = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt());
        hospitalSet.setSignKey(key);
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    @ApiOperation(value = "带查询条件的分页")
    @PostMapping(value = "/page/{pageNum}/{size}")
    public R getPageInfo(@PathVariable Integer pageNum,
                         @PathVariable Integer size,
                         @RequestBody HospitalSetQueryVo hospitalSetQueryVo){
        // ValueObject VO类 controller层和页面进行交互时使用 只接收医院名称和医院编号 防止属性浪费和泄露敏感字段
        // @RequestBody 将前端发送过来的JSON数据（也就是查询条件）转换为Pojo对象
        // GetMapping不支持@RequestBody @PostMapping大多协议为json格式，使用application/json格式
        Page<HospitalSet> page = new Page<>(pageNum,size);// 要根据HospitalSet找到对应的hospital_set表
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(hospitalSetQueryVo.getHosname()))
            queryWrapper.like("hosname",hospitalSetQueryVo.getHosname());
        if(!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode()))
            queryWrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        hospitalSetService.page(page, queryWrapper);
        return R.ok().data("total",page.getTotal()).data("rows",page.getRecords());
    }
    @ApiOperation(value = "查询所有的医院设置信息")
    @GetMapping(value = "/findAll")
    public R findAll(){
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("items",list);
    }

    // 根据医院设置ID删除医院设置信息
    @ApiOperation(value = "根据医院设置ID删除医院设置信息")
    @DeleteMapping(value = "/deleteById/{id}")
    public R deleteById(@PathVariable Integer id){
        try {
            hospitalSetService.removeById(id);
        } catch (Exception e) {
            throw new YyghException(20001,"Yygh异常:删除失败");
        }
        return R.ok();
    }

}

